﻿/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#ifndef _GCTL_SPHERE_H
#define _GCTL_SPHERE_H

#include "vertex.h"
#include "entity.h"

namespace gctl
{
    // Declaration of the basic sphere type
    template <typename A> struct type_sphere;
    // sphere type of attribute type of void
    typedef type_sphere<void> sphere;

	/**
	 * @brief      球型结构体
	 * 
	 * 包含球体的序号，球心位置与半径
	 */
    template <typename A>
	struct type_sphere : public entity<vertex3dc, 1, A>
	{
		double rad;
        /**
         * Constructor
         */
		type_sphere();
        /**
         * @brief      Set object from parameters
         *
         * @warning    This function will locate memories to store vertice
         *
         * @param[in]  p0     The central point
         * @param[in]  in_rad In radians
         * @param[in]  index  The element index
         */
        type_sphere(const point3dc &p0, double in_rad, int index = 0);
        /**
         * @brief      Set object's parameters
         *
         * @param      vert0     The central vertex
         * @param[in]  in_rad    In radians
         * @param[in]  index     The index
         */
		type_sphere(vertex3dc &vert0, double in_rad, int index = 0);
        /**
         * @brief      De-constructor
         */
		virtual ~type_sphere(){}
        /**
         * @brief      Set object from parameters
         *
         * @warning    This function will locate memories to store vertice
         *
         * @param[in]  p0     The central point
         * @param[in]  in_rad In radians
         * @param[in]  index  The element index
         */
        void set(const point3dc &p0, double in_rad, int index = 0);
        /**
         * @brief      Set object's parameters
         *
         * @param      vert0     The central vertex
         * @param[in]  in_rad    In radians
         * @param[in]  index     The index
         */
		void set(vertex3dc &vert0, double in_rad, int index = 0);
        /**
         * @brief      Reset the structure to initial state
         */
        void reset();
	};

    template <typename A>
    type_sphere<A>::type_sphere() : entity<vertex3dc, 1, A>::entity()
    {
        rad = 0.0;
    }

    template <typename A>
    type_sphere<A>::type_sphere(const point3dc &p0, double in_rad, int index) : type_sphere()
    {
        set(p0, in_rad, index);
    }

    template <typename A>
    type_sphere<A>::type_sphere(vertex3dc &vert0, double in_rad, int index) : type_sphere()
    {
        set(vert0, in_rad, index);
    }

    template <typename A>
    void type_sphere<A>::set(const point3dc &p0, double in_rad, int index)
    {
        if (index < 0)
        {
            throw out_of_range("Invalid index number, From type_sphere::set(...)");
        }

        if (in_rad <= 0.0)
        {
            throw runtime_error("Invalid radius. From type_sphere::set(...)");
        }

        this->vert[0] = new vertex3dc;
        this->self_host = true;

        this->id = index;
        this->vert[0]->set(p0, index);
        rad = in_rad;
        return;
    }

    template <typename A>
    void type_sphere<A>::set(vertex3dc &vert0, double in_rad, int index)
    {
        if (index < 0)
        {
            throw out_of_range("Invalid index number, From type_sphere::set(...)");
        }

        if (in_rad <= 0.0)
        {
            throw runtime_error("Invalid radius. From type_sphere::set(...)");
        }

        this->id = index;
        this->vert[0] = &vert0;
        rad = in_rad;
        return;
    }

    template <typename A>
    void type_sphere<A>::reset()
    {
        entity<vertex3dc, 1, A>::reset();
        rad = 0.0;
        return;
    }

    template <typename A, typename B>
	void copy_type_sphere(type_sphere<A> *tar, const type_sphere<B> *src)
	{
		copy_entity(tar, src);
        tar->rad = src->rad;
		return;
	}
}

#endif // _GCTL_SPHERE_H